package com.ah.shanxi_demo.param.result;

import java.io.Serializable;

/**
 * Created by zhangtao
 * 2019/7/18
 * <p>
 * 全民钱包 返回通用类
 */
public class SXResult<T> implements Serializable {
    private String code;
    private String msg;
    private T data;
    private Integer total;

    private SXResult() {
        this.code = SXErrorCode.SUCCESS.getCode();
        this.msg = SXErrorCode.SUCCESS.getMsg();
    }

    private SXResult(T data) {
        this.code = SXErrorCode.SUCCESS.getCode();
        this.msg = SXErrorCode.SUCCESS.getMsg();
        this.setData(data);
    }

    public SXResult(T data, Integer total) {
        this.code = SXErrorCode.SUCCESS.getCode();
        this.msg = SXErrorCode.SUCCESS.getMsg();
        this.data = data;
        this.total = total;
    }

    private SXResult(String code, String msg) {
        this.code = SXErrorCode.SUCCESS.getCode();
        this.msg = SXErrorCode.SUCCESS.getMsg();
        this.setCode(code);
        this.setMsg(msg);
    }

    private SXResult(String code, String msg, T data) {
        this.code = SXErrorCode.SUCCESS.getCode();
        this.msg = SXErrorCode.SUCCESS.getMsg();
        this.setCode(code);
        this.setMsg(msg);
        this.setData(data);
    }

    private SXResult(SXErrorCode dsqErrorCode) {
        this.code = dsqErrorCode.getCode();
        this.msg = dsqErrorCode.getMsg();
        this.setCode(code);
        this.setMsg(msg);
    }

    public SXResult<T> setError(String code, String msg) {
        this.setCode(code);
        this.setMsg(msg);
        return this;
    }

    public SXResult<T> setError(SXErrorCode errorCode) {
        this.setCode(errorCode.getCode());
        this.setMsg(errorCode.getMsg());
        return this;
    }

//    public boolean isSuccess() {
//        return StringUtils.equals(this.getCode(), SXErrorCode.SUCCESS.getCode());
//    }

    public static <T> SXResult instance() {
        return new SXResult();
    }

    public static <T> SXResult instance(SXErrorCode errorCode) {
        return new SXResult(errorCode);
    }

    public static <T> SXResult instance(T data) {
        return new SXResult(data);
    }

    public static <T> SXResult instance(T data, Integer total) {
        return new SXResult(data, total);
    }

    public static <T> SXResult instance(String code, String msg) {
        return new SXResult(code, msg);
    }

    public static <T> SXResult instance(String code, String msg, T data) {
        return new SXResult(code, msg, data);
    }

    public Integer getCode() {
        if (code != null) {
            if (code instanceof String) {
                return Integer.parseInt(code);
            }
        }
        return 0;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return this.msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return this.data;
    }

    public void setData(T data) {
        this.data = data;
    }


    public Integer getTotal() {
        if (total == null) {
            return 0;
        }
        return total;
    }

    public void setTotal(Integer total) {
        this.total = total;
    }

    public static void main(String[] args) {

        String msgs = "0";

        if (msgs instanceof String) {
            System.out.println("string");
        }
        System.out.println("int");
    }

}
